<template>
  <div>
    <div class="container">
      <address-head title="地址列表"></address-head>
    <van-address-list
      v-model="chosenAddressId"
      :list="contacts"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="changeContact"
    />
    </div>
  </div>
</template>

<script>
import AddressHead from '@components/AddressHead'
import { mapState, mapMutations } from 'vuex'
export default {
  data () {
    return {
      chosenAddressId: '1'
    }
  },
  methods: {
    onAdd () {
      this.$router.push('/addAddress')
    },
    onEdit (item, index) {
      this.$router.push({
        name: 'editAddress',
        params: {
          index
        }
      })
    },
    // 重新选定订单的联系人地址
    changeContact (contact, index) {
      // console.log(contact, index)
      this.changeNowOrderAddress(contact)
      this.chosenAddressId = contact.id
      this.$router.push('/order')
    },
    ...mapMutations('order', ['changeNowOrderAddress'])
  },
  computed: {
    // chosenAddressId () {
    //   // const searchDefault = this.contacts.find(el => el.isDefault)
    //   // if (searchDefault) {
    //   //   return searchDefault.id
    //   // } else {
    //   console.log(this.contacts)
    //   return this.contacts[0].id
    //   // }
    // },
    ...mapState({
      contacts: (state) => state.address.contacts
    })
  },
  components: {
    AddressHead
  }
}
</script>

<style lang="scss" scoped>

</style>
